package com.ywl.study.sharding.study06.factory;

import com.ywl.study.sharding.common.enums.ShardingType;
import com.ywl.study.sharding.study06.config.precise.MasterSlaveConfiguration;
import com.ywl.study.sharding.study06.config.precise.ShardingDatabasesAndTablesConfigurationPrecise;
import com.ywl.study.sharding.study06.config.precise.ShardingDatabasesConfigurationPrecise;
import com.ywl.study.sharding.study06.config.precise.ShardingMasterSlaveConfigurationPrecise;
import com.ywl.study.sharding.study06.config.precise.ShardingTablesConfigurationPrecise;

import javax.sql.DataSource;
import java.sql.SQLException;

public class DataSourceFactory {
    public static DataSource newInstance(final ShardingType shardingType) throws SQLException {
        switch (shardingType){
            case SHARDING_DATABASES:
                return new ShardingDatabasesConfigurationPrecise().getDataSource();
            case SHARDING_TABLES:
                return new ShardingTablesConfigurationPrecise().getDataSource();
            case SHARDING_DATABASES_AND_TABLES:
                return new ShardingDatabasesAndTablesConfigurationPrecise().getDataSource();
            case SHARDING_MASTER_SLAVE:
                return new ShardingMasterSlaveConfigurationPrecise().getDataSource();
            case MASTER_SLAVE:
                return new MasterSlaveConfiguration().getDataSource();
             default:
                 throw new UnsupportedOperationException();
        }
    }
}
